package com.jieshuibao.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.config.Global;
import com.jieshuibao.model.db.H5SpreadSign;
import com.jieshuibao.model.db.H5SpreadSignExample;
import com.jieshuibao.model.db.H5SpreadSignExample.Criteria;
import com.jieshuibao.service.H5SpreadSignService;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.h5.po.H5SpreadRes;

@Controller
@RequestMapping("/api")
public class H5SpreadSignController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/h5SpreadSign/list.do";
	
	@Resource
	private H5SpreadSignService h5SpreadSignService;
	
	@RequestMapping("/h5SpreadSign/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		H5SpreadSignExample h5SpreadSignExample = new H5SpreadSignExample();
		int totalCount = h5SpreadSignService.countH5SpreadSign(h5SpreadSignExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		h5SpreadSignExample.setPageIndex((pageNum - 1) * pageSize);
		h5SpreadSignExample.setPageSize(pageSize);
		List<H5SpreadSign> h5SpreadSigns = h5SpreadSignService.getPageH5SpreadSign(h5SpreadSignExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("h5SpreadSigns", h5SpreadSigns);
		return "/H5SpreadSign/list";
	}
	
	@RequestMapping("/h5SpreadSign/toAdd")
	public String toAdd(Model model) {
		return "/H5SpreadSign/add";
	}
	/**
	 * 报名课程接口
	 * @param model
	 * @param h5SpreadSign
	 * @return
	 */
	@RequestMapping(value="/h5SpreadSign/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(Model model, @RequestBody H5SpreadSign h5SpreadSign) {
		H5SpreadRes res = new H5SpreadRes();
		if(null == h5SpreadSign){
			res.setCode(Global.FAIL);
			res.setDes("参数不能为空");
			return res;
		}
		String phone = h5SpreadSign.getPhone();
		if(StringUtils.isEmpty(phone)){
			res.setCode(Global.FAIL);
			res.setDes("手机号不能为空！");
			return res;
		}
		if(!StringUtils.isMobile(phone)){
			res.setCode(Global.FAIL);
			res.setDes("手机号格式错误！");
			return res;
		}
		String courseId = h5SpreadSign.getCourseId();
		if(StringUtils.isEmpty(courseId)){
			res.setCode(Global.FAIL);
			res.setDes("未选择课程！");
			return res;
		}
		String email = h5SpreadSign.getEmail();
		if(StringUtils.isNotEmpty(email)){
			if(!StringUtils.checkEmail(email)){
				res.setCode(Global.FAIL);
				res.setDes("邮箱格式错误！");
				return res;
			}
		}
		H5SpreadSignExample example = new H5SpreadSignExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phone);
		criteria.andCourseIdEqualTo(courseId);
		List<H5SpreadSign> spreadSigns = h5SpreadSignService.selectByExample(example);
		if(null != spreadSigns && spreadSigns.size()>0){
			res.setCode(Global.FAIL);
			res.setDes("该手机号已经报名该课程！");
			return res;
		}
		String regCode = h5SpreadSign.getRegCode();
		if(StringUtils.isNotEmpty(regCode)){
			h5SpreadSign.setRegCode(regCode);
		}
		h5SpreadSign.setId(UUIDGenerator.getUUID());
		h5SpreadSign.setSignTime(new Date());
		h5SpreadSignService.addH5SpreadSign(h5SpreadSign);
		res.setCode(Global.SUCCESS);
		res.setDes("报名成功");
		return res;
	}

	@RequestMapping("/h5SpreadSign/del")
	public String del(Model model,
			@RequestParam(value = "phone", required = true) String phone) {
		h5SpreadSignService.delH5SpreadSignByPhone(phone);
		return redirect;
	}

	@RequestMapping("/h5SpreadSign/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "phone", required = true) String phone) {
		H5SpreadSign h5SpreadSign = h5SpreadSignService.getH5SpreadSignByPhone(phone);
		model.addAttribute("h5SpreadSign", h5SpreadSign);
		return "/H5SpreadSign/edit";
	}

	@RequestMapping("/h5SpreadSign/edit")
	public String edit(Model model, @ModelAttribute("h5SpreadSign") H5SpreadSign h5SpreadSign) {
		h5SpreadSignService.updateH5SpreadSign(h5SpreadSign);
		return redirect;
	}

	@RequestMapping("/h5SpreadSign/view")
	public String view(Model model,
			@RequestParam(value = "phone", required = true) String phone) {
		H5SpreadSign h5SpreadSign = h5SpreadSignService.getH5SpreadSignByPhone(phone);
		model.addAttribute("h5SpreadSign", h5SpreadSign);
		return "/H5SpreadSign/view";
	}
}
